const isNode = typeof window === 'undefined'

const SYMBOL = isNode
    ? global.Symbol
    : window.Symbol
        ? window.Symbol
        : (() => {
            function random () {
                const c = 'abcdefghijklmnopqrstuvwxyz0123456789'
                const cl = c.length
                let r = ''
                for (let i = 0; i < 10; i++) {
                    r += c[Math.floor(Math.random() * cl)]
                }
                return r
            }
            return (name) => {
                if (name) {
                    return `#${name}_${random()}`
                } else {
                    return `#_${random()}`
                }
            }
        })()

const mem: Map<any, any> = new Map()

const symbolManager = {
    get (name) {
        if (name) {
            return mem.has(name)
                ? mem.get(name)
                : (() => {
                    mem.set(name, SYMBOL(name))
                    return mem.get(name)
                })()
        } else {
            return SYMBOL('default')
        }
    }
}

export default symbolManager
